package com.ellwood.IPGuard.common.core.basic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import lombok.AllArgsConstructor;

import java.util.List;

/**
 * @ProjectName: IPGuard-boot
 * @PackageName: com.ellwood.IPGuard.common.core.basic
 * @ClassName: EntityRepositoryImpl
 * @Description: // 基础领域实体仓库接口实现
 * @Author: Ellwood
 * @CreateDate: 2024/9/25 - 下午3:54
 * @Version: V1.0.0
 */
@AllArgsConstructor
public abstract class EntityRepositoryImpl<T> implements EntityRepository<T> {
        
        protected final BaseMapper<T> baseMapper;
        
        // 添加无参构造函数
        protected EntityRepositoryImpl() {
                this.baseMapper = null; // 或者抛出异常，确保子类能初始化
        }
        
        @Override
        public List<T> findUserAll() {
                QueryWrapper<T> wrapper = new QueryWrapper<>();
                wrapper.eq("status", 0).eq("deleted", 0);
                if (baseMapper != null) {
                        return baseMapper.selectList(wrapper);
                }
                return List.of();
        }
        
        @Override
        public T findUserById(Long id) {
                return baseMapper.selectById(id);
        }
        
        @Override
        public boolean saveUser(T t) {
                return baseMapper.insert(t) > 0;
        }
        
        @Override
        public boolean editUser(T t) {
                return baseMapper.update(t, null) > 0;
        }
        
        @Override
        public boolean deleteUser(Long id) {
                return baseMapper.deleteById(id) > 0;
        }
        
        @Override
        public boolean deleteUserAll(List<Long> ids) {
                return baseMapper.deleteByIds(ids) > 0;
        }
}
